<template>
  <div>
    <span>获取验证码({{ time }}s)</span>
  </div>
</template>

<script setup lang="ts">
//引入组合式 API 函数 ref
import { ref, onMounted } from 'vue'
defineOptions({
  name: 'CountDown'
})
//倒计时事件
let time = ref<number>(5)
const flag = defineModel()
onMounted(() => {
  console.log('onMounted: ', onMounted)
  //开启定时器
  let timer = setInterval(() => {
    time.value--
    if (time.value == 0) {
      flag.value = false
      //清除定时器
      clearInterval(timer)
    }
  }, 1000)
})
</script>
